<?php
require_once('Vega/Dao/Table.php');
class Mobitv_Admin_NewsDao extends Vega_Dao_Table {
    /*
     * Hàm set active cho tin
     * @param       int $newsId
     */
    public function activeNews($newsId) {
        $newsId = intval($newsId);
        $sql = "UPDATE news SET active=0 WHERE id <> $newsId";
        $this->_db->queryWrite($sql);

        $sql = "UPDATE news SET active=1 WHERE id=$newsId";
        return $this->_db->queryWrite($sql);
    }

    /*
     * Hàm set active cho tin thời sự
     * @param       int $newsId
     */
    public function activeBreakingNews($videoId) {
        $videoId = intval($videoId);
        $sql = "UPDATE clip SET breaking_news=0 WHERE id<>$videoId";
        $this->_db->queryWrite($sql);

        $sql = "UPDATE clip SET breaking_news=1 WHERE id=$videoId";
        return $this->_db->queryWrite($sql);
    }

    /*
     * Hàm lấy danh sách tin tức
     * @param       int $page
     * @param       int $limit
     *
     * @return      array
     *                  int     $total
     *                  array   $data
     */
    public function getNews($page, $limit) {
        $offset = ($page - 1) * $limit;
        $sql = "SELECT id, title, active
                FROM news
                ORDER BY id DESC
                LIMIT $offset, $limit
                ";
        $countSQL = "SELECT COUNT(id) as total FROM news";
        $this->_db->query($countSQL);
        $row = $this->_db->fetchRow();
        $total = intval($row['total']);
        if ($total == 0) {
            $data = array();
        } else {
            $this->_db->query($sql);
            $data = $this->_db->fetchRowSet();
        }

        return array(
            'total' => $total,
            'data'  => $data
        );
    }

    /*
     * Hàm lấy danh sách tin tức thời sự
     * @param       int $page
     * @param       int $limit
     *
     * @return      array
     *                  int     $total
     *                  array   $data
     */
    public function getBreakingNews($page, $limit) {
        $offset     = $limit * ($page - 1);
        $_limit     = ' LIMIT ' . $limit;
        $_offset    = ' OFFSET ' . $offset;

        $sql = "SELECT * FROM clip WHERE active=1 AND deleted=0 AND (category_id=1 OR category_id=39) ORDER BY id DESC $_limit $_offset";
        $this->_db->query($sql);
        
        return $this->_db->fetchRowset();
    }

    public function countAllBreakingNews()
    {
        $sql = "SELECT count(*) AS 'count' FROM clip WHERE category_id=1 OR category_id=39";
        $this->_db->query($sql);
        $row = $this->_db->fetchRow();
        
        return $row ? $row['count'] : 0;
    }


    /*
     * Hàm lấy data của một tin
     * @param       int $newsId
     */
    public function getDetail($newsId) {
        $newsId = intval($newsId);
        $sql = "SELECT * FROM news WHERE id=$newsId";
        $this->_db->query($sql);
        return $this->_db->fetchRow();
    }
    
    /*
     * Hàm đăng mới một tin
     * @param       array $data
     */
    public function add($data) {
        $adminId = intval($_SESSION['userid']);
        $url = addslashes($data['url']);
        $title = addslashes($data['title']);
        $headline = addslashes($data['headline']);
        $content = addslashes($data['content']);

        $sql = "INSERT INTO news
                SET url='$url',
                    title = '$title',
                    headline = '$headline',
                    content = '$content',
                    updated_time = now(),
                    updated_by = $adminId,
                    thumbnail_version = 0";
        return $this->_db->queryWrite($sql);
    }

    public function getLastInsertId() {
        return $this->_db->getLastInsertId();
    }
    
    /*
     * Hàm lưu data của một tin
     * @param       int $newsId
     * @param       array $data
     * @param       bool [$updateThumbnailVersion]
     */
    public function save($newsId, $data, $updateThumbnailVersion = false) {
        $newsId = intval($newsId);
        $url = addslashes($data['url']);
        $adminId = intval($_SESSION['userid']);
        $title = addslashes($data['title']);
        $headline = addslashes($data['headline']);
        $content = addslashes($data['content']);


        if ($updateThumbnailVersion) {
            $sql = "UPDATE news
                    SET url = '$url',
                        title = '$title',
                        headline = '$headline',
                        content  = '$content',
                        updated_time = now(),
                        updated_by = $adminId,
                        thumbnail_version = thumbnail_version+1
                    WHERE id=$newsId";
        } else {
            $sql = "UPDATE news
                    SET url = '$url',
                        title = '$title',
                        headline = '$headline',
                        content  = '$content',
                        updated_time = now(),
                        updated_by = $adminId
                    WHERE id=$newsId";
        }
        return $this->_db->queryWrite($sql);
    }


    /*
     * Hàm xóa một list các tin theo id
     * @param       array $list (int) [assume that not empty]
     */
    public function deleteFromList($list) {
        $list = implode(",", $list);
        $sql = "DELETE FROM news WHERE id IN ($list)";
        return $this->_db->queryWrite($sql);
    }
}